﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using EntityObject;
using SharedLibraries;
using ThangNM_LIB.FUNC;

namespace DataAccessObject
{
    public class CategoryDAO : DB_SQL_EXT
    {
        public Boolean Insert_Update_Delete(ref CategoryEO _CategoryEO, Int16 Insert_Update_Delete)
        {
            SqlCommand cmd = new SqlCommand("Category_Insert_Update_Delete");
            cmd.CommandType = CommandType.StoredProcedure;
            //Insert tblSEO
            setParamSEO(_CategoryEO, ref cmd);

            cmd.Parameters.Add(new SqlParameter("@FK_iCategoryID", _CategoryEO.FK_iCategoryID));
            cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", _CategoryEO.FK_iAccountID));
            cmd.Parameters.Add(new SqlParameter("@FK_sTopicID", _CategoryEO.FK_sTopicID));
            cmd.Parameters.Add(new SqlParameter("@iType", _CategoryEO.iType));
            cmd.Parameters.Add(new SqlParameter("@sNameNonUnicode", _CategoryEO.sNameNonUnicode));
            cmd.Parameters.Add(new SqlParameter("@Insert_Update_Delete", Insert_Update_Delete));
            _CategoryEO.PK_iSEO = ExecuteNonQuery_RetInt32(cmd);
            //CustomerCache.lstCategory_ZipEO = null;
            if (_CategoryEO.PK_iSEO > 0)
                return true;
            else
                return false;
        }

        //public  Boolean Update(CategoryEO _CategoryEO)
        //{
        //    SqlCommand cmd = new SqlCommand("Category_Update", GetConnection());
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@PK_iTableID", _CategoryEO.PK_iTable));
        //    cmd.Parameters.Add(new SqlParameter("@PK_iSEOID", _CategoryEO.PK_iSEO));
        //    cmd.Parameters.Add(new SqlParameter("@FK_sAccountID", _CategoryEO.FK_sAccount));
        //    cmd.Parameters.Add(new SqlParameter("@FK_sCategoryID", _CategoryEO.FK_sCategory));
        //    cmd.Parameters.Add(new SqlParameter("@sTitle", NullEx.getNullValue(_CategoryEO.sTitle)));
        //    cmd.Parameters.Add(new SqlParameter("@sKeyword", NullEx.getNullValue(_CategoryEO.sKeyword)));
        //    cmd.Parameters.Add(new SqlParameter("@sDescription", NullEx.getNullValue(_CategoryEO.sDescription)));
        //    cmd.Parameters.Add(new SqlParameter("@sLinkMedia", NullEx.getNullValue(_CategoryEO.sLinkMedia)));
        //    cmd.Parameters.Add(new SqlParameter("@sLinkImage", NullEx.getNullValue(_CategoryEO.sLinkImage)));
        //    cmd.Parameters.Add(new SqlParameter("@sLinkImageThumbnail", NullEx.getNullValue(_CategoryEO.sLinkImageThumbnail)));
        //    cmd.Parameters.Add(new SqlParameter("@sSourceCode", NullEx.getNullValue(_CategoryEO.sSourceCode)));
        //    cmd.Parameters.Add(new SqlParameter("@sUrlRewrite", NullEx.getNullValue(_CategoryEO.sUrlRewrite)));
        //    cmd.Parameters.Add(new SqlParameter("@sUrlShort", NullEx.getNullValue(_CategoryEO.sUrlShort)));
        //    cmd.Parameters.Add(new SqlParameter("@iChangefreq", NullEx.getNullValue(_CategoryEO.iChangefreq)));
        //    cmd.Parameters.Add(new SqlParameter("@iRobots", NullEx.getNullValue(_CategoryEO.iRobots)));
        //    cmd.Parameters.Add(new SqlParameter("@fPriority", NullEx.getNullValue(_CategoryEO.fPriority)));
        //    cmd.Parameters.Add(new SqlParameter("@bStatus", NullEx.getNullValue(_CategoryEO.bStatus)));


        //    cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", _CategoryEO.FK_iAccountID));
        //    cmd.Parameters.Add(new SqlParameter("@FK_iCategoryID", _CategoryEO.FK_iCategoryID));
        //    cmd.Parameters.Add(new SqlParameter("@FK_sTopicID", ","));
        //    cmd.Parameters.Add(new SqlParameter("@sNameNonUnicode", _CategoryEO.sNameNonUnicode));
        //    cmd.Parameters.Add(new SqlParameter("@sCategoryEx", _CategoryEO.sCategoryEx));
        //    cmd.Parameters.Add(new SqlParameter("@iType", _CategoryEO.iType));
        //    ExecuteNonQuery(cmd);
        //    CustomerCache.lstCategory_ZipEO = null;
        //    return true;
        //}

        //public  Boolean Delete(string _PK_iCategoryID, String _loc_old)
        //{
        //    SqlCommand cmd = new SqlCommand("Category_Delete", GetConnection());
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@PK_iCategoryID", _PK_iCategoryID));
        //    cmd.Parameters.Add(new SqlParameter("@loc_old", _loc_old));
        //    ExecuteNonQuery(cmd);
        //    CustomerCache.lstCategory_ZipEO = null;
        //    return true;
        //}

        public CategoryEO SelectItem(Int16 _PK_iCategoryID)
        {
            SqlCommand cmd = new SqlCommand("Category_SelectItem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iCategoryID", _PK_iCategoryID));
            return CategoryDO(GetDataTable(cmd));
        }

        public List<Category_ZipEO> SelectBy_Zip()
        {
            List<Category_ZipEO> result = new List<Category_ZipEO>();
            SqlCommand cmd = new SqlCommand("Category_SelectBy_Zip");
            cmd.CommandType = CommandType.StoredProcedure;
            result = (List<Category_ZipEO>)ConvertEx.ConvertDataTableToListObject<Category_ZipEO>(GetDataTable(cmd), result.GetType());
            return result;
        }

        public DataTable SelectBy_bStatus(Boolean _bState)
        {
            SqlCommand cmd = new SqlCommand("Category_SelectBy_bState");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@bState", _bState));
            return GetDataTable(cmd);
        }

        public DataTable Category_Topic(Int16 _PK_iTable, String _FK_sTopicID)
        {
            SqlCommand cmd = new SqlCommand("Category_Topic_New");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iTable", NullEx.getNullValue(_PK_iTable)));
            cmd.Parameters.Add(new SqlParameter("@FK_sTopicID", NullEx.getNullValue(_FK_sTopicID)));
            return GetDataTable(cmd);
        }

        public CategoryEO SelectBy_sQueryString_sUrl(String _QueryString, String _sUrl)
        {
            SqlCommand cmd = new SqlCommand("Category_SelectBy_sQueryString_sUrl");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@sQueryString", NullEx.getNullValue(_QueryString)));
            cmd.Parameters.Add(new SqlParameter("@sUrl", NullEx.getNullValue(OtherEx.RemoveDomainInUrl(_sUrl).Split('?').FirstOrDefault())));
            return CategoryDO(GetDataTable(cmd));
        }

        public CategoryEO CategoryDO(DataTable input)
        {
            List<CategoryEO> lst = new List<CategoryEO>();
            lst = (List<CategoryEO>)ConvertEx.ConvertDataTableToListObject<CategoryEO>(input, lst.GetType());
            if (CommonsCheck.ListObject_OK(lst))
                return lst[0];
            else
                return null;
        }
    }
}